import copy

from src.log.logger import logger
from datetime import datetime, timezone
from config.constants import   SPDXVERSION, DATALICENSE, SPDXID, \
                        CREATEINFO
from src.tools.query import RpmInfoQuery

class SbomTopElementGenerate:
    """
    generate sbom top element info for rpm
    """

    def __init__(self, top_element, rpm_name):
        """
        init top_element, name
        Args:
            top_element:
            name:
        Returns:
        """

        self.top_element = top_element
        self.rpm_name = rpm_name
        
    def generate_top_element(self):
        """
        merge new sbom  element jsons to sbom.json
        Args:
        Returns:
        """
        self.top_element['spdxVersion'] = SPDXVERSION
        self.top_element['dataLicense'] = DATALICENSE
        self.top_element['SPDXID'] = SPDXID
        self.top_element['name'] = RpmInfoQuery.get_name_with_version(self.rpm_name)
        self.top_element['documentNamespace'] = self._get_document_namespace()
        self.top_element['creationInfo'] = copy.copy(CREATEINFO)
        self.top_element['creationInfo']['created'] = self._get_created_time()
        return self.top_element
    
    
        
    def _get_document_namespace(self):
        """
        get documentNamespace field
        Args:
        
        Returns:
        
        """
        
        return "https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS-SP3/everything/aarch64/Packages"
        
        
    def _get_created_time(self):
        """
        
        Args:
        
        Returns:
        
        """
        # 获取当前的UTC时间
        now_utc = datetime.now(timezone.utc)
        # 使用strftime格式化时间（但注意，这里不包括Z）
        formatted_time = now_utc.strftime('%Y-%m-%dT%H:%M:%S')
        # 手动添加Z表示UTC时区
        created_time = formatted_time + 'Z'
        return created_time